package remote

import (
	"context"
	"errors"
	"fmt"
	"github.com/spf13/cast"
	"log"
	"xiuxian_help_tool/backend/service/remote/model"
	"xiuxian_help_tool/backend/utils/sdk"
	xiuXianba "xiuxian_help_tool/backend/utils/sdk/xiuxianba"
)

// GetCardList 获取卡片列表
func GetCardList(page, size int, batchId, accessKey string) (resp *model.UsersInviteCardList, err error) {
	ctx := context.Background()
	var res *sdk.APIRes[*model.UsersInviteCardList]
	err = xiuXianba.XiuXianCli.Post("/backend/api/v1/card/list").
		SetBody(model.CardListSearchReq{
			PageSize:  size,
			Page:      page,
			BatchId:   batchId,
			AccessKey: accessKey,
		}).
		Do(ctx).Into(&res)
	if err != nil {
		return
	}
	if res != nil && res.Code == 0 {
		return nil, errors.New("获取卡片列表失败")
	}
	return res.Data, nil
}

func FindCardByUqKey(uqKey int64) (*model.UsersInviteCardDetail, error) {
	// /backend/api/v1/card/detail/uqKey/{id}
	ctx := context.Background()

	var res *sdk.APIRes[*model.UsersInviteCardDetail]
	err := xiuXianba.XiuXianCli.Get("/backend/api/v1/card/detail/uqKey/{id}/{type}").
		SetPathParam("id", cast.ToString(uqKey)).
		SetPathParam("type", "1").
		Do(ctx).Into(&res)
	if err != nil {
		log.Println(fmt.Sprintf("卡片不存在: %d", uqKey))
		return nil, err
	}
	if res != nil && res.Code == 0 {
		return nil, errors.New("获取卡片详情失败")
	}
	return res.Data, nil
}
